package com.lpf.www.admincontroller;

import com.lpf.www.common.dto.ResponseMsg;
import com.lpf.www.common.entity.Admin;
import com.lpf.www.common.utils.CommonData;
import com.lpf.www.common.utils.DataHandler;
import com.lpf.www.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author lpf
 * @Date 2021/12/30 下午12:07
 * @ProjectName live-streaming-os
 * @Version 1.0
 * @Description: 管理员的控制层代码
 */
@Controller
@RequestMapping("admin")
public class AdminController {

    private AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping("adminLogin")
    @ResponseBody
    public String adminLogin(String username, String password) {
        ResponseMsg responseMsg = new ResponseMsg();
        if (DataHandler.isEmpty(username)) {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("用户名不能为空！");
        } else if (DataHandler.isEmpty(password)) {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("密码不能为空！");
        } else {
            Admin admin = new Admin();
            admin.setUsername(username);
            admin.setPassword(password);
            responseMsg = adminService.adminLogin(admin);
        }
        return DataHandler.getJsonString(responseMsg);
    }

    @RequestMapping(value = "adminRegister")
    @ResponseBody
    public String adminRegister(String username, String password, String confirmPassword, String adminName) {
        ResponseMsg responseMsg = new ResponseMsg();
        if (DataHandler.isEmpty(username)) {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("用户名不能为空！");
        } else if (DataHandler.isEmpty(password)) {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("密码不能为空！");
        } else if (DataHandler.isEmpty(adminName)) {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("管理员姓名不能为空！");
        } else if (!password.equals(confirmPassword)) {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("两次输入密码不一致！");
        } else {
            Admin admin = new Admin();
            admin.setUsername(username);
            admin.setPassword(password);
            admin.setName(adminName);
            responseMsg = adminService.insertAdmin(admin);
        }
        return DataHandler.getJsonString(responseMsg);
    }
}
